package com.room.controller.student;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.room.pojo.DormResult;
import com.room.pojo.NodisClass;
import com.room.pojo.Student;
import com.room.pojo.User;
import com.room.pojo.vo.StudentImportVO;
import com.room.pojo.vo.StudentVO;
import com.room.service.StudentService;
//import com.room.utils.ExcelUtils;
import com.room.service.UserService;
import com.room.utils.FileUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人 lin
 * @创建时间 2019/10/7
 * @描述
 */
@RestController
@RequestMapping("/stu/basic")
public class StudentController {
    @Autowired
    StudentService studentService;

    @Autowired
    UserService userService;


    /**
     * 通过学号查询到学生的接口
     * @param studentNo
     * @return
     */
    @RequestMapping(value = "getStudentByNo",method = RequestMethod.GET)
    public Map<String,Object> getStudentByNo(@RequestParam String studentNo){
        Map<String,Object> resultMap = new HashMap<String,Object>();
        if(studentService.getStudentByNo(studentNo)==null){
            resultMap.put("notice","no");
            return resultMap;
        }
      resultMap.put("notice",studentService.getStudentByNo(studentNo));
        return resultMap;
    }

    @RequestMapping(value = "/stu",method = RequestMethod.PUT)
    public DormResult updateStudent(Student student){
        if(studentService.update(student)==1){
            return DormResult.ok("修改成功");
        }
        else{
            return DormResult.error("修改失败");
        }
    }

//    @RequestMapping(value = "/getStuByColId" ,method = RequestMethod.GET)
//    public List<Student> getStudentByCollege(@RequestParam  String collegeId){
//        return studentService.getStudentByCollegeId(collegeId);
//    }
    @RequestMapping(value = "/Nodis",method = RequestMethod.GET)
    public Map<String,Object> findByNodis(@RequestParam String collegeId,@RequestParam String grade){
        Map resultMap = new HashMap<String,Object>();
     List<NodisClass>  nodisClasses =  studentService.findByNodis(collegeId,grade);
        resultMap.put("nodisClasses",nodisClasses);
        return resultMap;
    }

    /**
     * 获得学生的分页数据
     * @param page
     * @param size
     * @return返回学生的分页数据
     */
    @RequestMapping(value = "/stu",method = RequestMethod.GET)
    public Map<String,Object> getStudentByPage(
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "6")Integer size
    ){

        Map<String,Object> map = new HashMap<String,Object>();
        List<Student> students = studentService.getStudentByPage(page,size);
        Long total = studentService.getStudentCount();
        map.put("stus",students);
        map.put("total",total);
        return map;
    }

    /**
     * 删除指定id的学生
     * @param ids
     * @return
     */
    @RequestMapping(value = "/stu/{ids}",method = RequestMethod.DELETE)
    public DormResult deleteById(@PathVariable String ids){
        if(studentService.deleteStuById(ids)){
            return DormResult.ok("删除成功");
        }
        return DormResult.error("删除失败");
    }

//  public List<Student> getStudentByCollegeId(@RequestParam String collegeId){
//
//  }

    /**
     * 获取指定学院学生的数据
     * @param collegeId
     * @return
     */
    @RequestMapping(value = "/stuByCollegeId",method = RequestMethod.GET)
    public Map<String,Object> getStudentByCollegeId(@RequestParam String collegeId){
      Map<String,Object> resultMap = new HashMap<String,Object>();
      List<Student> students = studentService.getStudenrByCollegeName(collegeId);
      long total = studentService.getTotal(collegeId);
      resultMap.put("students",students);
      resultMap.put("total",total);
      return resultMap;
    }


    @RequestMapping(value = "exportStu",method = RequestMethod.GET)
    public void toExport(HttpServletResponse response) {
        List<StudentVO> studentVOS = studentService.getAllStudentVO();
        FileUtils.exportExcel(studentVOS, "学生公寓信息表 title" , "学生公寓信息表 sheet", StudentVO.class, "学生信息公寓表.xls", response);
    }

    @RequestMapping(value = "/importStu",method = RequestMethod.POST)
    public void toHtmlOf03Img(@RequestParam MultipartFile file) throws IOException, InvalidFormatException {
        ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);//表头行数,默认1
        importParams.setTitleRows(1);//表格标题行数,默认0
//        importParams.setNeedVerify(false);// 需要验证
   try{
     List<StudentImportVO> studentImportVOS = ExcelImportUtil.importExcel(file.getInputStream(),StudentImportVO.class,importParams);
     Student student = new Student();
     User user = new User();
       for(StudentImportVO  studentImportVO : studentImportVOS){
           student.setBedroomName(studentImportVO.getBedroomName());
           student.setBedroomId(studentImportVO.getBedroomId());
           student.setStatus(studentImportVO.getStatus());
           student.setStudentName(studentImportVO.getStudentName());
           student.setClassName(studentImportVO.getClassName());
           student.setCollegeId(studentImportVO.getCollegeId());
           student.setCollegeName(studentImportVO.getCollegeName());
           student.setClassId(studentImportVO.getClassId());
           student.setGrade(studentImportVO.getGrade());
           student.setSex(studentImportVO.getSex());
           String maxID = studentService.getMaxId();
           int nowNo = Integer.parseInt(maxID)+1;
           String maxNo = String.valueOf(nowNo);
           student.setStudentNo(maxNo);
           studentService.insert(student);
           Long nowId = userService.getUserMaxId();
           user.setName(studentImportVO.getStudentName());
           user.setUsername(maxNo);
           BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
           String encode = bCryptPasswordEncoder.encode("dgut"+maxNo);
           user.setPassword(encode);
           userService.insertUser(user);
           long ridnow = 4;
           Long[] rid = new Long[]{ridnow};
           userService.addRolesForUser(nowId+1,rid);
       }

   }catch (IOException e){
       System.out.println("导入失败");
   }catch (Exception e){}
    }

}
